/**
 * @file map_manager.cpp
 * @author shaojiale
 * @brief
 * @version 0.1
 * @date 2022-07-21
 *
 * @copyright Copyright (c) 2022
 *
 */
#include "map_manager.h"

#include <cstdio>
#include <iostream>
#include <yaml-cpp/yaml.h>

namespace cadmus {
namespace map {

using namespace rapidjson;

bool getMark(std::string map_id, std::string layer_id, std::string mark_id)
{
}

bool MapManager::getMarks(std::string map_id, std::string layer_id)
{
}

bool MapManager::getLayer(std::string map_id, std::string layer_id)
{
}

std::string MapManager::getLayers(std::string map_id)
{
    auto doc = load_layers(map_id);

    StringBuffer               buffer;
    PrettyWriter<StringBuffer> writer(buffer);
    doc.Accept(writer);
    return buffer.GetString();
}

std::string MapManager::getGridInfo(std::string map_id)
{
    auto doc = load_grid_info(map_id);

    StringBuffer               buffer;
    PrettyWriter<StringBuffer> writer(buffer);
    doc.Accept(writer);
    return buffer.GetString();
}

int MapManager::getGridSize(std::string map_id)
{
    //读取压缩文件，获取文件大小和分片包
    std::ifstream map_fin(map_id + ".pgm", std::ios::binary | std::ios::ate);
    if (!map_fin)
    {
        return 0;
    }
    return map_fin.tellg();
}
Document MapManager::load_map(std::string map_id)
{
    Document doc;
    doc.SetObject();
    auto info   = load_grid_info(map_id);
    auto grid   = load_grid(map_id);
    auto layers = load_layers(map_id);
    doc.AddMember("info", info, doc.GetAllocator());
    doc.AddMember("grid", grid, doc.GetAllocator());
    doc.AddMember("layers", load_layers(map_id), doc.GetAllocator());
    return doc;
}

Document MapManager::load_layers(std::string map_id)
{
    Document doc;
    doc.SetObject();
    std::ifstream  ifs(map_id + ".json");
    IStreamWrapper isw(ifs);
    doc.ParseStream(isw);
    return doc;
}

Document MapManager::load_grid(std::string map_id)
{
    Document doc;
    doc.SetObject();
    return doc;
}

Document MapManager::load_grid_info(std::string map_id)
{
    Document doc;
    doc.SetObject();
    YAML::Node map_yaml;
    try
    {
        map_yaml = YAML::LoadFile(map_id + ".yaml");
    }
    catch (const std::exception &e)
    {
        std::cout << "Load " + map_id + ".yaml Failed!" << std::endl;
    }
    StringBuffer               buffer;
    PrettyWriter<StringBuffer> writer(buffer);
    writer.StartObject();
    writer.Key("image");
    writer.String(map_yaml["image"].as<std::string>().c_str());
    writer.Key("resolution");
    writer.Int(static_cast<int>(map_yaml["resolution"].as<double>() * 1000));
    writer.Key("origin");
    writer.StartArray();
    writer.Int(static_cast<int>(map_yaml["origin"].as<std::vector<double>>()[0] * 1000));
    writer.Int(static_cast<int>(map_yaml["origin"].as<std::vector<double>>()[1] * 1000));
    writer.Int(static_cast<int>(map_yaml["origin"].as<std::vector<double>>()[2] * 1000));
    writer.EndArray();
    writer.Key("negate");
    writer.Int(map_yaml["negate"].as<int>());
    writer.Key("occupied_thresh");
    writer.Double(map_yaml["occupied_thresh"].as<double>());
    writer.Key("free_thresh");
    writer.Double(map_yaml["free_thresh"].as<double>());
    writer.EndObject();
    doc.Parse(buffer.GetString());
    return doc;
}
} // namespace map
} // namespace cadmus